home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / GRAPHICS / GIF2RPC.SPK / source / 16bpp_66bit / c / stucki < prev    next >
Text File  |  1995-10-16  |  4KB  |  122 lines

  1. /* stucki.c
  2.  * AUTHOR:      Cy Booker, cy@cheepnis.demon.co.uk
  3.  * LICENSE:     FreeWare, Copyright (c) 1995 Cy Booker
  4.  *
  5.  * filter:              *  8  4
  6.  *                2  4  8  4  2
  7.  *                1  2  4  2  1         (1/42)
  8.  */
  9.  
  10. #include "internal.h"
  11.  
  12. #include <assert.h>
  13. #include <string.h>             /* memset() */
  14. #include <stdlib.h>             /* calloc() */
  15.  
  16. #include "OS:hourglass.h"
  17. #include "OS:macros.h"
  18.  
  19.  
  20. /* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  21.  */
  22.  
  23. extern bool process_gif_16bpp_stucki_66bit(
  24.                 const process_gif       *p) {
  25.   byte                  *rove;
  26.   int                   width, height;
  27.   int                   x, y;
  28.   const os_colour       *palette;
  29.   int                   line_length;
  30.   int                   *buffer, *this_row, *next_row, *botm_row;
  31.   int                   buffer_width;
  32.   int                   r, g, b;
  33.   int                   or, og, ob;
  34.   int                   t;
  35.   int                   er, eg, eb;
  36.  
  37.   assert(p);
  38.   assert(p->pixel_width > 0);
  39.   assert(p->pixel_height > 0);
  40.   assert(p->in_palette.colours);
  41.  
  42.   /*
  43.    * stucki requires storing error info for two pixels to right and two pixels to left
  44.    * so we will just allocate two extra columns for each row and not do any edge checks
  45.    * each entry is stored as {r*SCALE, g*SCALE, b*SCALE}
  46.    */
  47.   buffer_width = (2 + p->pixel_width + 2) * 3;
  48.   buffer = calloc(1, sizeof(*buffer) * (buffer_width * 3));
  49.   if (!buffer) {
  50.     /*
  51.      * oh dear
  52.      */
  53.     return TRUE;
  54.   }
  55.  
  56.   /*
  57.    * not we pre-load values from the process_gif array
  58.    * because it considerably helps the compiler produce better code
  59.    */
  60.   rove = p->buffer;
  61.   width = p->pixel_width;
  62.   height = p->pixel_height;
  63.   palette = p->in_palette.colours;
  64.   line_length = p->line_length;
  65.   for (y= height; (y > 0); y--) {
  66.     if ((y & 7) == 0) {
  67.       xhourglass_percentage((y * 100) / height);
  68.     }
  69.     this_row = buffer + (buffer_width * ((y + 3) % 3)) + 2*3;
  70.     next_row = buffer + (buffer_width * ((y + 2) % 3)) + 2*3;
  71.     botm_row = buffer + (buffer_width * ((y + 1) % 3)) + 2*3;
  72.     /* bottom row has no errors */
  73.     memset(botm_row, 0, sizeof(*botm_row) * (buffer_width - 2*3));
  74.     /*
  75.      * note that just because we are actually scanning/outputting right to left
  76.      * doesn't matter as far as the filter is concerned
  77.      * although it might help if we could ``snake''
  78.      */
  79.     for (x= width - 1; (x >= 0); x--) {
  80.       INPUT;
  81.       r += *this_row++;                                 /* add in errors from all rows */
  82.       g += *this_row++;
  83.       b += *this_row++;
  84.       PROCESS;
  85.       r -= or;  g -= og; b-= ob;                        /* error */
  86.       er = (r * 8) / 42; eg = (g * 8) / 42; eb = (b * 8) / 42;
  87.       this_row[ 0] += er; this_row[ 1] += eg; this_row[ 2] += eb;       /* this[ 1] += 8/42 */
  88.       next_row[ 0] += er; next_row[ 1] += eg; next_row[ 2] += eb;       /* next[ 0] += 8/42 */
  89.       r -= 2 * er; g -= 2*eg; b -= 2*eb;                /* remainder of r error left */
  90.  
  91.       er >>= 1; eg >>= 1; eb >>= 1;
  92.       this_row[ 3] += er; this_row[ 4] += eg; this_row[ 5] += eb;       /* this[ 2] += 4/42 */
  93.       next_row[ 3] += er; next_row[ 4] += eg; next_row[ 5] += eb;       /* next[ 1] += 4/42 */
  94.       next_row[-3] += er; next_row[-2] += eg; next_row[-1] += eb;       /* next[-1] += 4/42 */
  95.       botm_row[ 0] += er; botm_row[ 1] += eg; botm_row[ 2] += eb;       /* botm[ 0] += 4/42 */
  96.       r -= 4 * er; g -= 4 * eg; b -= 4 * eb;
  97.  
  98.       er >>= 1; eg >>= 1; eb >>= 1;
  99.       next_row[ 6] += er; next_row[ 7] += eg; next_row[ 8] += eb;       /* next[ 2] += 2/42 */
  100.       next_row[-6] += er; next_row[-5] += eg; next_row[-4] += eb;       /* next[-2] += 2/42 */
  101.       botm_row[ 3] += er; botm_row[ 4] += eg; botm_row[ 5] += eb;       /* botm[ 1] += 2/42 */
  102.       botm_row[-3] += er; botm_row[-2] += eg; botm_row[-1] += eb;       /* botm[-1] += 2/42 */
  103.       r -= 4 * er; g -= 4 * eg; b -= 4 * eb;
  104.  
  105.       er >>= 1; eg >>= 1; eb >>= 1;
  106.       botm_row[-6] += er; botm_row[-5] += eg; botm_row[-4] += eb;       /* botm[-2] += 1/42 */
  107.  
  108.       r -= er; g -= eg; b -= eb;
  109.       botm_row[ 6] += r;  botm_row[ 7] += g;  botm_row[ 8] += b;        /* botm[+2] += 1/42 */
  110.  
  111.       next_row += 3;                                    /* adjust next_row pointer */
  112.       botm_row += 3;                                    /* adjust next_row pointer */
  113.     }
  114.     rove += line_length;
  115.   }
  116.   free(buffer);
  117.   return FALSE;
  118. }
  119.  
  120.  
  121.  
  122.